/**
 * 
 */
package com.pmsco.pms.core.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.pmsco.pms.core.dao.PmsMenuDao;
import com.pmsco.pms.core.domain.PmsMenu;
import com.pmsco.pms.util.JQGridBuildWhereClause;

/**
 * @author Long Tran
 *
 */
public class PmsMenuDaoImpl extends HibernateDaoSupport implements PmsMenuDao {

	public PmsMenu getMenuById(Long id) {
		PmsMenu ret = getHibernateTemplate().load(PmsMenu.class, id);
		return ret;
	}
	
	public List<PmsMenu> listPmsMenu(PmsMenu searchTemplate) {
		List<PmsMenu> ret = new ArrayList<PmsMenu>();
		Criteria crit = this.getSession().createCriteria(PmsMenu.class);
		crit.setFetchMode("children", FetchMode.SELECT);
		crit.setFetchMode("parent", FetchMode.SELECT);
		if(searchTemplate.getIsTopLevel() != null)
			crit.add(Restrictions.eq(PmsMenu.isTopLevelField, searchTemplate.getIsTopLevel()));
		crit.addOrder(Order.asc(PmsMenu.orderIdField));
		ret = crit.list();
		return ret;
	}
	
	public List<PmsMenu> listPmsMenu(String searchField, String searchString, String searchOper, String sidx, String sord, Integer page, Integer rows) {
		List<PmsMenu> ret = new ArrayList<PmsMenu>();
		Criteria crit = this.getSession().createCriteria(PmsMenu.class);
		crit.setFetchMode("children", FetchMode.SELECT);
		crit.setFetchMode("parent", FetchMode.SELECT);
		if(searchOper != null && searchOper.length() > 0)
			crit.add(JQGridBuildWhereClause.getWhereClase(searchField, searchString, searchOper));
		if("desc".equalsIgnoreCase(sord))
			crit.addOrder(Order.desc(sidx));
		else
			crit.addOrder(Order.asc(sidx));
		crit.setFirstResult((page - 1) * rows);
		crit.setMaxResults(page * rows);
		ret = crit.list();
		return ret;
	}
	
	public Integer countListPmsMenu(String searchField, String searchString, String searchOper) {
		Integer ret = new Integer(0);
		Criteria crit = this.getSession().createCriteria(PmsMenu.class);
		crit.setFetchMode("children", FetchMode.SELECT);
		crit.setFetchMode("parent", FetchMode.SELECT);
		if(searchOper != null && searchOper.length() > 0)
			crit.add(JQGridBuildWhereClause.getWhereClase(searchField, searchString, searchOper));
		crit.setProjection(Projections.rowCount());
		ret = (Integer)crit.uniqueResult();
		return ret;
	}
	
	public List<PmsMenu> findByProperty(String propertyName, Object value) {
		try {
			String queryString = "from " + PmsMenu.className + " as model where model."
					+ propertyName + "= ? order by model." + PmsMenu.orderIdField;
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	public void saveOrUpdatePmsMenu(PmsMenu pmsMenu) {
		getHibernateTemplate().merge(pmsMenu);
		getHibernateTemplate().flush();
	}
	
	public void deleteMenu(PmsMenu pmsMenu) {
		getHibernateTemplate().delete(pmsMenu);
		getHibernateTemplate().flush();
	}
}
